package com.test.web.controller.vue;

import com.he.core.ResultT;
import com.he.web.annotation.NeedToken;
import com.test.web.bean.AllBean;
import com.test.web.bean.vue.req.LoginReq;
import com.test.web.bean.vue.res.LoginRes;
import com.test.web.bean.vue.res.user.InfoRes;
import com.test.web.controller.base.BaseController;
import com.test.web.service.BackuserService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author heyanjing
 * date:2020-06-09 2020/6/9:22:54
 */
@Slf4j
@AllArgsConstructor
@RestController
public class UserController extends BaseController {

    @Autowired
    private BackuserService backuserService;

    @NeedToken(false)
    @PostMapping("/user/login")
    public ResultT<LoginRes> login(@RequestBody LoginReq bean) {
        return backuserService.login(bean);
    }

    @GetMapping("/user/info")
    public ResultT<InfoRes> info() {
        return this.backuserService.info(super.getBackuser());
    }

    @NeedToken(false)
    @GetMapping("/user/logout")
    public ResultT logout() {
        // 退出登录，清除redis缓存
        return ResultT.success();
    }

    @GetMapping("/user/refresh")
    public ResultT refresh() {
        return this.backuserService.refresh(super.getBackuser());
    }


    @GetMapping("/load")
    public ResultT load() {
        List<AllBean> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            list.add(new AllBean().setId(i + 1));
        }

        return ResultT.successWithData(list);
    }

}
